function createTitlebarDragRegion(theme) {
    const dragRegion = document.createElement("div");
    dragRegion.className = "titlebar-drag-region";
    dragRegion.classList.add(theme);
    return dragRegion;
}

function getTheme() {
    var value = localStorage.getItem('.drawio-config');
    if (value != null) {
        return JSON.parse(value).ui || null;
    }
    return window.uiTheme;
}

function createWindowsControls() {
    const theme = getTheme();
    const controlsContainer = document.createElement("div");
    controlsContainer.className = "window-controls-container " + theme;
    controlsContainer.innerHTML = `
            <div class="window-icon-bg">
                <div class="window-icon window-minimize"></div>
            </div>
            <div class="window-icon-bg">
                <div class="window-icon window-max-restore"></div>
            </div>
            <div class="window-icon-bg window-close-bg">
                <div class="window-icon window-close"></div>
            </div>
        `;
    document.body.appendChild(controlsContainer);

    const electron = require("electron");
    const currentWindow = electron.remote.getCurrentWindow();
    const minimize = controlsContainer.querySelector(".window-minimize");
    const maxRestore = controlsContainer.querySelector(".window-max-restore");
    const winClose = controlsContainer.querySelector(".window-close");

    minimize.addEventListener("click", () => currentWindow.minimize());

    maxRestore.classList.add(currentWindow.isMaximized() ? "window-unmaximize" : "window-maximize");
    currentWindow.on("maximize", () => {
        maxRestore.classList.remove("window-maximize");
        maxRestore.classList.add("window-unmaximize");
    });
    currentWindow.on("unmaximize", () => {
        maxRestore.classList.remove("window-unmaximize");
        maxRestore.classList.add("window-maximize");
    });
    maxRestore.addEventListener("click", () => {
        if (currentWindow.isMaximized()) {
            currentWindow.unmaximize();
        } else {
            currentWindow.maximize();
        }
    });

    winClose.addEventListener("click", () => {
        electron.remote.app.exit();
    });
    return controlsContainer;
}

function reLayout() {
    const theme = getTheme();
    const menuContainer = document.querySelector(".geMenubarContainer");
    if (menuContainer) {
        // 隐藏语言图标
        if (menuContainer.previousElementSibling) {
            menuContainer.previousElementSibling.style.display = "none";
        }
        if (theme !== "sketch") {
            // 插入标题栏
            menuContainer.prepend(createTitlebarDragRegion(theme));
        }

        const menubar = menuContainer.querySelector(".geMenubar");
        if (menubar) {
            menubar.style.top = "0";
            menubar.nextElementSibling.nextElementSibling.style.top = "30px";
        }
    }
    const controls = createWindowsControls();
    // 插入标题栏
    if (theme === "sketch") {
        document.body.insertBefore(createTitlebarDragRegion(theme), controls);
    }
}
